//
//  XSJWelcomeViewController.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/13.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
import SDWebImage
class XSJWelcomeViewController: UIViewController {
    
    
    private lazy var bgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var headImageView : UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        view.layer.cornerRadius = 45
        view.layer.masksToBounds = true
        if let url = XSJUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            view.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return view
    }()
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        
        if let name = XSJUserAccountViewModel.sharedUserAccount.userAccount?.name{
            label.text = name + "欢迎回来!"
            
        }else
        {
            label.text = "欢迎回来!"
        }
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        SetUpUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func SetUpUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
    }
    private func startAnimation () {
        
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        messageLabel.alpha = 0
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //  让子视图重写布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //  c进入到首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
    }
    
}